package leetcode.f1t100;

import java.util.HashMap;

/**
 * 数组nums内寻找相加等于target的两个数下标
 *
 * https://leetcode.com/problems/two-sum/
 *
 * @author lichx
 * @date 2021/9/14 16:35
 */
public class Q1_TwoSum {
    public static void main(String[] args) {

    }

    public int[] twoSum1(int[] nums, int target) {
        for (int i = 0; i < nums.length - 1; i++) {
            for (int j = i + 1; j < nums.length; j++) {
                if (nums[i] + nums[j] == target) {
                    return new int[]{i, j};
                }
            }
        }
        return new int[]{-1, -1};
    }


    public int[] twoSum(int[] nums, int target) {
        HashMap<Integer, Integer> subResult = new HashMap<>();
        for (int i = 0; i < nums.length; i++) {
            if (subResult.containsKey(target - nums[i])) {
                return new int[]{subResult.get(target - nums[i]), i};
            }
            subResult.put(nums[i], i);
        }
        return null;
    }


}
